$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true
			,queryIng : false//正在查询
			,queryData:{activeFlag:'1',page:1,rows:10,moduleCode:'',orderColum:"order_index"}//查询条件
			,datagrid:[]//数据列表数据
			,totalNum:0//分页控件配置
			,data:[]
			,defaultCheckedKeys:[]
			,addData:{moduleCode:''}
			,selectedRow:null
			,isEdit:false
			,dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
				,excelDialogVisible:false
				,saveAble:true
			}
			,rules:{
				title:[
					{ required: true, message: '请输入标题', trigger: 'change' }
				]
				,prerequisite:[
					{ required: true, message: '请输入前提条件', trigger: 'change' }
				]
				,caseSteps:[
					{ required: true, message: '请输入用例步骤', trigger: 'change' }
				]
				,expectedResult:[
					{ required: true, message: '请输入预期结果', trigger: 'change' }
				]
				,libraryName:[
					{ required: true, message: '请选择用例库', trigger: 'change' }
				]
				,moduleCode:[
					{ required: true, message: '请选择模块', trigger: 'change' }
				]
				,type:[
					{ required: true, message: '请选择用例类型', trigger: 'change' }
				]
				,level:[
					{ required: true, message: '请选择优先级', trigger: 'change' }
				]
			}
			,libraryData:[]
			,moduleData:[]
			,typeData:[]
			,levelData:[]
			,cascaderData:[]
	        ,defaultProps: {
	            children: 'children',
	            label: 'text',
	            value: "id"
	        }
			,cascaderValue:[]
			,createNext:false
		},
		methods: {
			init:function () {
				var vm = this
				vm.query();
				vm.loadLibraryData();
			}
			,cascaderChange:function(data){
				app.queryData.libraryCode = data[0];
				if(data.length>1){
					app.queryData.moduleCode = data[1];
				}else{
					app.queryData.moduleCode = null;
				}
			}
			,libraryChange:function(data){
				var vm = this
				vm.libraryData.find(function(item){
					if(item.id==data){
						vm.addData.libraryName = item.name
						return;
					}
				})
				vm.loadModuleData(data);
			    vm.moduleCode = null;
			    vm.addData.moduleCode = null;
			    vm.addData.libraryCode = data;
			    vm.queryData.moduleCode = null;
			}
			,moduleChange:function(data){
				var vm = this
				vm.addData.moduleCode = data;
				vm.moduleData.find(function(item){
					if(item.id==data){
						vm.addData.moduleName = item.name
						return;
					}
				}) 
			}
			,loadLibraryData:function(){
				var vm = this
				$.ajax({
					url :staticURL+'/testcase/caseProject/listAll?activeFlag=1',
					dataType : 'json',
					success : function(json) {
						vm.libraryData = json;
					}
				});
			}
			,loadModuleData:function(param){
				var vm = this
				var projectId = param;
				if(!param){
					return;
				}
				$.ajax({
					dataType : 'json',
					success : function(json) {
						app.moduleData = json;
					}
				});
			}
			,loadTypeData:function(){
				var vm = this
				vm.loadSelectData('A02');
			}
			,loadLevelData:function(){
				var vm = this
				vm.loadSelectData('A03');
			}
			,loadSelectData:function(parentCode){
				var vm = this
				$.ajax({
					url :staticURL+'/basic/baseInfoAction/combox?parentCode='+parentCode,
					dataType : 'json',
					success : function(json) {
						if(parentCode=='A02'){
							vm.typeData = json;
						}else if(parentCode=='A03'){
							vm.levelData = json;
						}
					}
				});
			}
			,query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				$.ajax({
					url :staticURL+'/testcase/caseLibrary/datagrid.do',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			}
			,addItem:function(){
				app.openDialog();
			}
			,deleteItem:function(){
				if(!app.selectedRow){
					app.$notify({showClose: true,
				          message: '请选择一条要删除的记录',
				          type: 'warning',
				          position: 'bottom-right'});
				}else{
					app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
							url : staticURL+"/testcase/caseLibrary/deletePt?ids="+app.selectedRow.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				}
				return;
				
			}
			,editItem:function(param){
				app.dialogConfig.saveAble = param
				if(!app.selectedRow){
					app.$notify({showClose: true,
				          message: '请选择一条要编辑的记录',
				          type: 'warning',
				          position: 'bottom-right'});
				}else{
					app.openDialog(app.selectedRow);
					if(app.selectedRow.libraryCode){
						app.loadSelectData(app.selectedRow.libraryCode);
						app.moduleCode = app.selectedRow.moduleName;
					}
				}
			}
			,handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			}
			,handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			}
			,handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			}
			,resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    }
			,submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						//验证通过
						$.ajax({
							url : app.saveUrl,
							data : app.addData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									if(app.createNext){
										app.resetForm('dialogForm');
										app.moduleCode = null;
										app.addData.moduleName = null;
									}else{
										app.dialogConfig.dialogFormVisible=false;
									}
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			}
			,openDialog:function(param){
				if(param){
					app.isEdit = true;
					//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
					//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
					//app.addDatat同时被reset,会导致bug
					app.addData = JSON.parse(JSON.stringify(param)); 
				}else{
					app.isEdit = false;
					app.addData = {orderNum:1};
				}
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//加载select
				app.loadTypeData();
				app.loadLevelData();
				app.createNext = false;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
				if(app.queryData.libraryCode){
					app.addData.libraryCode = app.queryData.libraryCode
				}
				if(app.queryData.moduleCode){
					app.addData.moduleCode = app.queryData.moduleCode
				}
				console.log(app.addData);
			}
			,closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
				app.resetForm('dialogForm')
			}
			,datagridSelect:function(row, event, column){
				app.selectedRow = row;
			}
			,tableCellStyle:function({row, column, rowIndex, columnIndex}){
				if(columnIndex == 5){
					if(row.level=='P1'){
						return 'background:#F56C6C'
					}
					if(row.level=='P2'){
						return 'background:#409EFF'
					}
					if(row.level=='P3'){
						return 'background:#67C23A'
					}
					if(row.level=='P4'){
						return 'background:#DCDFE6'
					}
					return ''
				}
			}
		    ,rowStyle({ row, rowIndex}) {
		        if (rowIndex === 0) {
		          return 'color:green'
		        } else {
		          return ''
		        }
		     }
		    ,exportExcel:function(){
		    	var libraryCode = "";
		    	if(app.queryData.libraryCode){
		    		libraryCode = app.queryData.libraryCode;
		    	}
		    	var moduleCode = "";
		    	if(app.queryData.libraryCode){
		    		moduleCode = app.queryData.moduleCode;
		    	}
		    	var title = "";
		    	if(app.queryData.title){
		    		title = app.queryData.title;
		    	}
		    	window.open(staticURL+"/testcase/caseLibrary/export?libraryCode="+libraryCode+"&moduleCode="+moduleCode+"&title="+title);
		    }
		    ,importExcel:function(){
		    	app.dialogConfig.excelDialogVisible = true;
		    	app.$refs['excelUploader'].clearFiles();
		    }
		    ,uploadExcelSuccess:function(response, file, fileList){
		    	if(response.success){
		    		app.dialogConfig.excelDialogVisible = false;
		    		app.$notify({showClose: true,
		    			message: '上传成功',
		    			type: 'success',
		    			position: 'bottom-right'});
		    		app.query();
		    	}else{
		    		app.$notify({showClose: true,
		    			message: response.msg,
		    			type: 'error',
		    			position: 'bottom-right'});
		    	}
		    	app.$refs['excelUploader'].clearFiles();
		    }
		    ,uploadExcelError:function(){
		    	app.$notify({showClose: true,
			          message: '上传失败',
			          type: 'error',
			          position: 'bottom-right'});
		    	app.$refs['excelUploader'].clearFiles();
		    }
		    ,datagridDbclick:function(row, event){
		    	app.editItem(false);
		    }
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/testcase/caseLibrary/edit'
				}else{
					return staticURL+'/testcase/caseLibrary/add'
				}
			}
		}
		,created: function () {
			this.init();
		},
		updated:function(){
		}
	});
});